<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }
        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .login-container input {
            display: block;
            width: 100%;
            margin-bottom: 10px;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .login-container button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .login-container button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <input type="text" id="username" placeholder="用户名" required>
    <input type="password" id="password" placeholder="密码" required>
    <!-- 记住我选项 -->
    <input type="checkbox" name="remember-me"/> 记住我
    <button onclick="login()">登录</button>
    <p id="message"></p>
</div>

<script>
    async function login() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        const formData = new FormData();
        formData.append('username', username);
        formData.append('password', password);

        try {
            const response = await fetch('/manage/login.do', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                throw new Error('登录失败');
            }

            const data = await response.json();
            if (data.success) {
                document.getElementById('message').textContent = '登录成功，正在跳转...';
                // 假设登录成功后，我们得到一个token，这里将其存储在localStorage中
                localStorage.setItem('token', data.token);
                // 跳转到home页面
                window.location.href = '/home.html';
            } else {
                document.getElementById('message').textContent = '登录失败，用户名或密码错误';
            }
        } catch (error) {
            document.getElementById('message').textContent = error.message;
        }
    }
</script>
</body>
</html>
